<?xml version="1.0" encoding="UTF-8" ?>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<title>Events</title>

	<script type="text/javascript" language="javascript" src="/resources/common/js/EventBus.js"></script>
	<script type="text/javascript" language="javascript" src="/resources/js/Constants.js"></script>
	<script type="text/javascript" language="javascript">
		window.onload = function() {
			// ******************************************************************************************				
			function myFunction(event) {
				console.log("myFunction type=" + event.type);
			}
			EventBus.addEventListener(EventType.ALARM_RECEIVED, myFunction);
			EventBus.dispatch(EventType.ALARM_RECEIVED);

			// ******************************************************************************************
			console.log("\n");
			var TestClass1 = function() {
				this.className = "TestClass1";
				this.callback = function(event) {
					console.log(this.className + " = type:" + event.type + " / dispatcher:" + event.target.className);
				}
			};
			var TestClass2 = function() {
				this.className = "TestClass2";
				this.dispatchOurEvent = function() {
					EventBus.dispatch("callback_event", this);
				}
			};
			var t1 = new TestClass1();
			var t2 = new TestClass2();
			EventBus.addEventListener("callback_event", t1.callback, t1);
			t2.dispatchOurEvent();

			// ******************************************************************************************
			console.log("\n");
			var TestClass1 = function() {
				this.className = "TestClass1";
				this.doSomething = function(event, param1, param2) {
					console.log(this.className + ".doSomething");
					console.log("type=" + event.type);
					console.log("params=" + param1 + param2);
					console.log("coming from=" + event.target.className);
				}
			};
			var TestClass2 = function() {
				this.className = "TestClass2";
				this.ready = function() {
					EventBus.dispatch("custom_event", this, "javascript events", " are really useful");
				}
			};

			var t1 = new TestClass1();
			var t2 = new TestClass2();

			EventBus.addEventListener("custom_event", t1.doSomething, t1);
			t2.ready();

			// ******************************************************************************************
			console.log("\n");
			var winterHandler = function() {
				console.log("arguments", arguments);
			}
			EventBus.addEventListener("let-it-snow", winterHandler, null, "and happy", "new year");
			EventBus.dispatch("let-it-snow", null, "merry", "christmas");
			EventBus.dispatch("let-it-snow", null, "merry", "christmas");
			EventBus.dispatch("let-it-snow", null, "merry", "christmas");

		}

	</script>
</head>
<body>
	<div id="console.log" style="height:300px">
	</div>
</body>
</html>